package com.tzinfo.tzt.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.util.Enumeration;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class HttpTools {

	public HttpTools() {
		// TODO Auto-generated constructor stub
	}

	public static String getJsonContent(String url_path) {
		try {
			URL url = new URL(url_path);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setConnectTimeout(60000);
			connection.setRequestMethod("GET");
			connection.setDoInput(true);
			int code = connection.getResponseCode();
			if (code == 200) {
				String result = changeInputStream(connection.getInputStream(),
						null);
				return result;
			}
			Log.e("code", code + "");
		} catch (Exception e) {
			return e.toString();
		}
		return "error";
	}

	public static String getJsonContent(String url_path, String coding) {
		try {
			URL url = new URL(url_path);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setConnectTimeout(60000);
			connection.setRequestMethod("GET");
			connection.setDoInput(true);
			int code = connection.getResponseCode();
			if (code == 200) {
				String result = changeInputStream(connection.getInputStream(),
						coding);
				return result;
			}
			Log.e("code", code + "");
		} catch (Exception e) {
			return e.toString();
		}
		return "error";
	}



	private static String changeInputStream(InputStream inputStream,
			String coding) {
		String jsonString = "";
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		int len = 0;
		byte[] data = new byte[1024];
		try {
			while ((len = inputStream.read(data)) != -1) {
				outputStream.write(data, 0, len);
			}
			if (coding == null) {
				jsonString = new String(outputStream.toByteArray());
			} else {
				jsonString = new String(outputStream.toByteArray(), coding);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jsonString;
	}

	public static String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			// System.out.println(ex.toString());
		}
		return null;
	}

	public static String uploadImage(String url,String filePath) {
		String failString = "{\"success\":" + false
				+ ",\"msg\":\"Error\",\"data\":\"\"})";
		try {
			File file=new File(filePath);
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(url);
			MultipartEntity reqEntity = new MultipartEntity(
					HttpMultipartMode.BROWSER_COMPATIBLE);
			Log.e("file", file.getAbsolutePath());
			FileBody img = new FileBody(file, "image/jpeg");
			reqEntity.addPart("PicFile", img);
			httppost.setEntity(reqEntity);
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity resEntity = response.getEntity();
			if (resEntity != null) {
				String page = EntityUtils.toString(resEntity);
				Log.e("tong", page);
				return page;
			}else{
				return failString;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("tong", e.toString());
			return e.toString();
		}
	}
}